package com.example.as.Mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.as.entity.Record;
import com.example.as.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.List;

@Repository
@Mapper
public interface UserMapper extends BaseMapper<User> {
    @Insert("INSERT INTO relation (user1, user2 ) VALUES (#{account1}, #{account2})")
    int addFri(String account1, String account2);

    @Select("SELECT * FROM user WHERE account = #{account}")
    List<User> selByUserAccount(String account);

    @Delete("DELETE FROM relation WHERE user1 = #{account1} AND user2 = #{account2}")
    int delByRelationAccount(String account1, String account2);

    @Select("SELECT * FROM user WHERE user.account = #{account} AND user.psw = #{psw}")
    List<User> loginIn(String account, String psw);

    @Insert("INSERT INTO user VALUES (null, #{account}, #{psw}, #{name})")
    int loginUp(String name, String account, String psw);

    @Select("SELECT r.user2 AS related_account, u.name AS related_name  \n" +
            "FROM relation r  \n" +
            "JOIN user u ON r.user2 = u.account  \n" +
            "WHERE r.user1 = #{account}  \n" +
            "UNION   \n" +
            "SELECT r.user1 AS related_account, u.name AS related_name  \n" +
            "FROM relation r  \n" +
            "JOIN user u ON r.user1 = u.account  \n" +
            "WHERE r.user2 = #{account};")
    @ResultMap("relatedUserResult")
    List<User> getUserList(String account);

    @Select("SELECT * FROM user WHERE account IN \n" +
            "(SELECT user2 FROM relation WHERE user1 = #{account} OR user2 = #{account} \n" +
            "UNION  \n" +
            "SELECT user1 FROM relation WHERE user1 = #{account} OR user2 = #{account})")
    List<User> getUserFriendList(String account);
}
